package com.feifan.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.feifan.api.model.OmsOrderReturnReason;
import com.feifan.api.model.OmsOrderReturnReasonExample;
import com.feifan.api.service.IOmsOrderReturnReasonService;
import com.feifan.common.CommonPage;
import com.feifan.provider.mapper.OmsOrderReturnReasonMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @program: FinalProject
 * @author: Xiaofan
 * @createTime: 2021-08-06 16:17
 * @description: Functions of this class is
 **/
@Service(
        version = "1.0.0",
        interfaceName = "com.feifan.api.service.IOmsOrderReturnReasonService",
        interfaceClass = IOmsOrderReturnReasonService.class
)
@Transactional
public class OmsOrderReturnReasonService implements IOmsOrderReturnReasonService {

    @Autowired
    private CacheManager cacheManager;//用于做增删改时将缓存内的数据删除，保持数据同步性
    @Autowired
    private OmsOrderReturnReasonMapper dao;

    //增加缓存记录
    @CachePut(cacheNames = {"OrderReturnReason"}, key = "#returnReason.id")//通过id往里面添加数据
    @Override
    public OmsOrderReturnReason create(OmsOrderReturnReason returnReason) {
        clearOrderReturnReason();//增删改前先对之前的缓存进行删除
        returnReason.setCreateTime(new Date());
        dao.insert(returnReason);

        return returnReason;
    }

    //删除缓存中的数据
    public void clearOrderReturnReason() {
        //即每次做完增删改操作后，都能把效果都展现出来 ***
        cacheManager.getCache("OrderReturnReasonList").clear();
    }


    //往缓存里存储
    @Cacheable(cacheNames = {"OrderReturnReasonList"},//一定要写除非unless，意思即数据都在上面那个缓存中存储，除非达到了unless中的条件，即不存储
            unless = "#result==null",//除非结果为空，即不缓存
            key = "#pageSize+'-'+#pageNum")//确保其结果为唯一值
    @Override
    public CommonPage list(Integer pageNum, Integer pageSize) {
        //进行分页配置
        PageHelper.startPage(pageNum, pageSize);
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.setOrderByClause("create_time");//对指定字段进行排序

        return CommonPage.restPage(dao.selectByExample(example));
    }

    //进行缓存的删除
    @CacheEvict(cacheNames = "OrderReturnReason",
            key = "ids[0]",//即取出要删除记录的一条来进行删除
            allEntries = true,//true的意思是删除时，将缓存里的所有的该条数据都给删除
            beforeInvocation = false)//即在删除MySQL之后，再删除缓存
    @Override
    public Integer delete(List<Long> ids) {
        clearOrderReturnReason();//增删改前先对之前的缓存进行删除
        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdIn(ids);
        return dao.deleteByExample(example);//返回删除的个数
    }

    //进行缓存数据的修改
    @CachePut(cacheNames = "OrderReturnReason", key = "#id")
    @Override
    public OmsOrderReturnReason update(Long id, OmsOrderReturnReason returnReason) {
        clearOrderReturnReason();//增删改前先对之前的缓存进行删除
        returnReason.setId(id);//通过该主键来去找到该数据
        dao.updateByPrimaryKey(returnReason);//然后根据主键进行修改
        return returnReason;//然后返回该条修改的记录
    }

    @Cacheable(cacheNames = "OrderReturnReason", key = "#id", unless = "#result==null")//内容为空则不缓存
    @Override
    public OmsOrderReturnReason getOmsOrderReturnReason(Long id) {
        return dao.selectByPrimaryKey(id);//通过主键找到所有数据
    }

    @CachePut(cacheNames = "OrderReturnReason", key = "#id", unless = "#result==null")
    @Override
    public OmsOrderReturnReason updateStatue(Long id, Integer status) {
        clearOrderReturnReason();//增删改前先对之前的缓存进行删除

        OmsOrderReturnReason returnReason = null;
        if (!status.equals(0) && !status.equals(1)) {//如果该数据状态码不为1或者0，则直接返回该对象
            return returnReason;
        }
        returnReason = getOmsOrderReturnReason(id);//先通过id值来找到该条数据 取出来
        returnReason.setStatus(status);//将新的状态设置好

        OmsOrderReturnReasonExample example = new OmsOrderReturnReasonExample();
        example.createCriteria().andIdEqualTo(id);//找到该记录
        dao.updateByExampleSelective(returnReason,example);
        return returnReason;
    }

}